package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.Sales;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.SalesService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/sales")
@Slf4j
public class SalesController {
    @Resource
    private SalesService salesService;

    @GetMapping
    @LogScope(title = "销售管理模块", content = "查看销售列表信息")
    public Result select(@RequestParam Integer current) {
        PageInfo<Sales> pageInfo = salesService.selectSalesByPage(current);
        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "销售管理模块", content = "根据id查看销售列表信息")
    public Result detail(@PathVariable String id) {
        Sales sales = salesService.selectById(id);
        return Result.success(sales);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "销售管理模块", content = "根据id删除销售信息")
    public Result delete(@PathVariable String id) {
        int i = salesService.deleteById(id);
        return i>0 ? Result.success():Result.error();
    }

    @PutMapping
    @LogScope(title= "销售管理模块", content = "修改销售信息")
    public Result edit(@RequestBody Sales sales) {
        int i =salesService.edit(sales);
        return i>0 ? Result.success():Result.error();
    }

    @PostMapping("/save")
    @LogScope(title= "销售管理模块", content = "添加销售信息")
    public Result save(@RequestBody Sales sales) {
        sales.setId(RandomUtil.getRandomNumber());
        int i = salesService.save(sales);
        return i>0 ? Result.success():Result.error();
    }
}
